#include "addtable.h"
#include "ui_addtable.h"

AddTable::AddTable(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AddTable)
{
    ui->setupUi(this);

    table=REF_TABLE;
}

AddTable::~AddTable()
{
    delete ui;
}

bool AddTable::addItem(const QString &tableName,
                       const QString &tableLabel)
{
    QSqlQuery query;
    query.prepare(QString("insert into %1 (table_name,table_label) values (?,?)")
                  .arg(table));
    query.addBindValue(tableName);
    query.addBindValue(tableLabel);
    return query.exec();
}

void AddTable::on_btn_submit_clicked()
{
    auto name=ui->tableName->text().trimmed();
    auto label=ui->tableLabel->text().trimmed();

    if(name.isEmpty() || label.isEmpty())
        return;

    if(addItem(name,label))
        accept();
    else {
        QMessageBox::warning(this,"异常情况","无法提交数据!");
    }
}
